unit EventQ;

interface

uses
  Database, Functions, AM,
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, Menus;

type
  TFormEventQ = class(TForm)
    BtnOK: TBitBtn;
    BtnCancel: TBitBtn;
    EditEvent: TEdit;
    label2: TLabel;
    Label1: TLabel;
    EditCommand: TEdit;
    MemoExamples: TMemo;
    Label3: TLabel;
    BtnFunctions: TBitBtn;
    EditCopy: TEdit;
    MainMenu1: TMainMenu;
    MenuInvisible: TMenuItem;
    MenuCtrlF: TMenuItem;
    BtnHelpVars: TBitBtn;
    MenuF1: TMenuItem;
    BtnShow: TSpeedButton;
    BtnAM: TBitBtn;
    Ctrla1: TMenuItem;
    procedure EditEventChange(Sender: TObject);
    procedure EditEventKeyPress(Sender: TObject; var Key: Char);
    procedure BtnCancelClick(Sender: TObject);
    procedure BtnOKClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure EditCommandKeyPress(Sender: TObject; var Key: Char);
    procedure MemoExamplesClick(Sender: TObject);
    procedure BtnFunctionsClick(Sender: TObject);
    procedure BtnHelpVarsClick(Sender: TObject);
    procedure BtnShowClick(Sender: TObject);
    procedure BtnAMClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    didcreate: boolean;
  end;

var
  FormEventQ: TFormEventQ;

implementation
uses
   DBClient;

{$R *.DFM}

procedure TFormEventQ.FormCreate(Sender: TObject);
begin
  didcreate:=false;
  height:=151;
end;

procedure TFormEventQ.BtnCancelClick(Sender: TObject);
begin
  close;
end;

procedure TFormEventQ.EditEventChange(Sender: TObject);
begin
  if length(EditEvent.text) > 0 then BtnOK.enabled:=true
  else BtnOK.enabled:=false;
end;

procedure TFormEventQ.EditEventKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    EditCommand.selectall;
    EditCommand.setfocus;
    key:=#0;
  end;
end;

procedure TFormEventQ.EditCommandKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    if BtnOK.enabled then BtnOK.click;
    key:=#0;
  end;
end;

procedure TFormEventQ.BtnOKClick(Sender: TObject);
const
  FILTER_STR = '(Master = %s) and (FGroup = ''BASE'')';
var
  replacing, didfind: boolean;
  etable, vtable: TClientDataset;
  work, pre, post, event, va, varstr, failure, failure2: string;
  vars, coms: TStringList;
  amt, ho, p, s, c, d, i, l, l2, j: integer;
  mr: TModalResult;
begin
  if EditEvent.text = '' then begin
    messagedlg('Please enter text for the event',mtinformation,[mbok],0);
    EditEvent.setfocus;
    exit;
  end;
  if pos('"',EditEvent.text) > 0 then begin
    messagedlg('Events cannot contain quotes',mtinformation,[mbok],0);
    EditEvent.selectall;
    EditEvent.setfocus;
    exit;
  end;
  if pos(EditEvent.text,EditCommand.text) > 0 then begin
    if messagedlg('Having the event text in the command might cause recursion.  Continue?',
      mtinformation,[mbyes,mbno],0) = mrno then begin
      EditCommand.selectall;
      EditCommand.setfocus;
      exit;
    end;
  end;
  etable:=TClientDataset.create(self);
  EditEvent.text:=Datawell.CutTrailingSpaces(EditEvent.text);
  EditCommand.text:=Datawell.CutTrailingSpaces(EditCommand.text);
  with etable do begin
    CloneCursor(datawell.TableEvents, false);
    filter := format(FILTER_STR, [Datawell.masterindex]);
    filtered := true;
    active:=true;
    first;
    amt:=0;
    ho:=0;
    replacing:=false;
    while not EOF do begin
      inc(amt);
      if fieldbyname('FOrder').asinteger > ho then ho:=fieldbyname('FOrder').asinteger;
      if fieldbyname('FEvent').asstring = EditEvent.text then replacing:=true;
      next;
    end;
  end;
  inc(ho);
  if replacing then begin
    if messagedlg('Event already exists.  Overwrite?',mtconfirmation,
      [mbyes,mbcancel],0) = mrcancel then begin
      EditEvent.selectall;
      EditEvent.setfocus;
      etable.free;
      exit;
    end;
  end;
  if (amt >= 25) and (not replacing) then begin
    messagedlg('You can only have 25 events defined in the BASE group group',
      mtinformation,[mbok],0);
    EditEvent.selectall;
    EditEvent.setfocus;
    etable.free;
    exit;
  end;
  event:=' '+EditEvent.text+' ';
  event:=Datawell.subs('*','@ ',event);
//check for variables
  varstr:='';
  p:=pos('@',event);
  while p > 0 do begin
    pre:=copy(event,1,p-1);
    work:=copy(event,p+1,99999);
    s:=pos(' ',work);
    c:=pos(':',work);
    if (s < 0) and (c < 0) then begin
      va:=work;
      post:='';
    end
    else if (s > 0) and (c > 0) then begin
      if c < s then begin
        va:=copy(work,1,c-1);
        post:=copy(work,c+1,99999);
      end
      else begin
        va:=copy(work,1,s-1);
        post:=copy(work,s,99999);
      end;
    end
    else if c > 0 then begin
      va:=copy(work,1,c-1);
      post:=copy(work,c+1,99999);
    end
    else begin
      va:=copy(work,1,s-1);
      post:=copy(work,s,99999);
    end;
    if va = '' then begin
      va:='<@>';
      delete(post,1,1);
      event:=pre+'*'+post;
    end
    else event:=pre+'*'+post;
    varstr:=varstr+' '+va;
    p:=pos('@',event);
  end;
  event:=Datawell.CutLeadingSpaces(event);
  event:=Datawell.CutTrailingSpaces(event);
  varstr:=Datawell.CutLeadingSpaces(varstr);
  varstr:=Datawell.CutTrailingSpaces(varstr);
  varstr:=Datawell.subs(' ',',',varstr);
  if pos('**',event) > 0 then begin
    messagedlg('Event variables/stars cannot be right next to one another',mtinformation,[mbok],0);
    EditEvent.selectall;
    EditEvent.setfocus;
    etable.free;
    exit;
  end;
  vtable:=TClientDataset.create(self);
  with vtable do begin
    clonecursor(Datawell.TableVariables, false);
    filtered := true;
    active:=true;
    vars:=TStringList.create;
    Datawell.explode(varstr,',',vars);
    for i:=0 to vars.count-1 do begin
      if vars[i] = '<@>' then continue;
      j:=Datawell.rvars.indexof(vars[i]);
      if j >= 0 then begin
        if Datawell.rvars[j] <> vars[i] then begin
          mr:=messagedlg('Use the system variable @'+Datawell.rvars[j]+' instead?',
            mtconfirmation,[mbyes,mbno,mbcancel],0);
          if mr = mrcancel then begin
            vars.free;
            vtable.free;
            etable.free;
            exit;
          end;
          if mr = mryes then begin
            EditCommand.text:=Datawell.subs('@'+vars[i],'@'+Datawell.rvars[j],EditCommand.text);
            vars[i]:=Datawell.rvars[j];
          end;
        end;
        if Datawell.rvars[j] = vars[i] then continue;
      end;
      if not Datawell.ValidWordNumber(vars[i]) then begin
        messagedlg('Variables can contain only alphanumeric characters',
          mtinformation,[mbok],0);
        EditEvent.selectall;
        EditEvent.setfocus;
        vars.free;
        vtable.free;
        etable.free;
        exit;
      end;
      first;
      didfind:=false;
      failure:='';
      failure2:='';
      while not EOF do begin
        l:=length(fieldbyname('FVariable').asstring);
        if fieldbyname('FVariable').asstring = vars[i] then begin
          didfind:=true;
          break;
        end
        else if (copy(vars[i],1,l) = fieldbyname('FVariable').asstring) and
          (vars[i] <> fieldbyname('FVariable').asstring) then begin
          failure:=fieldbyname('FVariable').asstring;
          break;
        end
        else if (copy(fieldbyname('FVariable').asstring,1,i) = vars[i]) and
          (vars[i] <> fieldbyname('FVariable').asstring) then begin
          failure:=fieldbyname('FVariable').asstring;
          break;
        end;
        next;
      end;
      if failure <> '' then begin
        messagedlg('New variables cannot be lenghtened versions of an existing variable'+#13+
          'The existing variable being compared against is: '+failure+#13+
          'Please choose a new variable to use',mtinformation,[mbok],0);
        EditEvent.selectall;
        EditEvent.setfocus;
        vars.free;
        vtable.free;
        etable.free;
        exit;
      end;
      if failure2 <> '' then begin
        messagedlg('New variables cannot be shortened versions of an existing variable'+#13+
          'The existing variable being compared against is: '+failure+#13+
          'Please choose a new variable to use',mtinformation,[mbok],0);
        EditEvent.selectall;
        EditEvent.setfocus;
        vars.free;
        vtable.free;
        etable.free;
        exit;
      end;
      if not didfind then begin
        append;
        fieldbyname('Master').asinteger:=Datawell.masterinteger;
        fieldbyname('FVariable').asstring:=vars[i];
        fieldbyname('Initialize').asstring:='<clear on login>';
        fieldbyname('FColor').asinteger:=clwhite;
        fieldbyname('BColor').asinteger:=clnavy;
        fieldbyname('Invisible').asboolean:=false;
        post;
      end;
    end;
    varstr:='';
    for i:=0 to vars.count-1 do varstr:=varstr+','+vars[i];
    system.delete(varstr,1,1);
    vars.free;
  end;
  vtable.free;
  if not replacing then begin
    with etable do begin
      first;
      while not EOF do begin
        if fieldbyname('FEvent').asstring = event then begin
          replacing:=true;
          break;
        end;
        next;
      end;
    end;
    if replacing then begin
      if messagedlg('Event already exists.  Overwrite?',mtconfirmation,
        [mbyes,mbcancel],0) = mrcancel then begin
        EditEvent.selectall;
        EditEvent.setfocus;
        etable.free;
        exit;
      end;
    end;
  end;
  with etable do begin
    if replacing then edit else append;
    fieldbyname('Master').asinteger:=Datawell.masterinteger;
    fieldbyname('FGroup').asstring:='BASE';
    fieldbyname('FEvent').asstring:=event;
    fieldbyname('FAction').asstring:=varstr;
    fieldbyname('Sound').asstring:='';
    fieldbyname('Soundfile').asstring:='';
    fieldbyname('FColor').asinteger:=claqua;
    fieldbyname('BColor').asinteger:=clteal;
    fieldbyname('CaseSensitive').asboolean:=false;
    fieldbyname('Terminal').asboolean:=false;
    fieldbyname('FOrder').asinteger:=ho;
    coms:=TStringList.create;
    coms.add(EditCommand.text);
    fieldbyname('Script').assign(coms);
    coms.free;
    post;
  end;
  etable.free;
  didcreate:=true;
  close;
end;

procedure TFormEventQ.MemoExamplesClick(Sender: TObject);
begin
  if BtnOK.enabled then BtnOK.setfocus else EditEvent.setfocus;
end;

procedure TFormEventQ.BtnFunctionsClick(Sender: TObject);
var
  form: TFormFunctions;
  i, po: integer;
begin
  form:=TFormFunctions.create(self);
  form.Setup(Datawell.lastfunction,EditCommand.seltext);
  form.showmodal;
  po:=form.po;
  form.free;
  i:=EditCommand.selstart;
  if po < 1 then begin
    EditCommand.setfocus;
    exit;
  end;
  EditCommand.pastefromclipboard;
  EditCommand.setfocus;
  EditCommand.sellength:=0;
  EditCommand.selstart:=i+po;
end;

procedure TFormEventQ.BtnHelpVarsClick(Sender: TObject);
begin
  Datawell.Help('processing_variables');
end;

procedure TFormEventQ.BtnShowClick(Sender: TObject);
begin
  height:=371;
  BtnShow.hide;
end;

procedure TFormEventQ.BtnAMClick(Sender: TObject);
var
  form: TFormAM;
  am: string;
begin
  form:=TFormAM.create(self);
  form.setup;
  form.showmodal;
  am:=form.am;
  form.free;
  if am = '' then exit;
  EditCommand.text:=am;
  EditCommand.setfocus;
end;

end.
